package com.kaidi.dibankecloud.widget;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.ColorDrawable;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.kaidi.dibankecloud.R;
import com.kaidi.dibankecloud.activity.RegisterActivity;
import com.kaidi.dibankecloud.databinding.PopupActivatedBinding;

/**
 * Created by Administrator on 2017/1/10.
 */

public class ActivatedNowPopupWindow extends PopupWindow implements View.OnClickListener{

    private PopupActivatedBinding binding;
    private Context context;
    private EditText[] codes;
    private View anchor;

    public ActivatedNowPopupWindow(Context context) {
        super(context);
        this.context = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.popup_activated, null, false);
        binding.activatedCancelBtn.setOnClickListener(this);
        binding.activatedConfirmBtn.setOnClickListener(this);
        codes = new EditText[]{binding.activatedEt1,binding.activatedEt2,binding.activatedEt3,binding.activatedEt4};
        binding.activatedEt1.requestFocus();
        binding.activatedEt1.addTextChangedListener(new ActivatedTextWatcher(0));
        binding.activatedEt2.addTextChangedListener(new ActivatedTextWatcher(1));
        binding.activatedEt3.addTextChangedListener(new ActivatedTextWatcher(2));
        binding.activatedEt4.addTextChangedListener(new ActivatedTextWatcher(3));
        setContentView(binding.getRoot());
        setWidth((int) context.getResources().getDimension(R.dimen.protocol_width));
        setHeight((int) context.getResources().getDimension(R.dimen.reg_success_height));
        ColorDrawable dw = new ColorDrawable(0000000000);
        setBackgroundDrawable(dw);
        setFocusable(true);
        update();
    }

    public void showPopupWindow(View anchor){
        this.anchor = anchor;
        if(!isShowing()){
            showAtLocation(anchor, Gravity.CENTER,0,0);
            Window window = ((Activity) context).getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.5f;
            window.setAttributes(lp);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activated_cancel_btn:
                dismiss();
                WindowManager.LayoutParams lp = ((RegisterActivity)context).getWindow().getAttributes();
                lp.alpha = 1f;
                ((RegisterActivity)context).getWindow().setAttributes(lp);
                break;
            case R.id.activated_confirm_btn:
                StringBuffer sb = new StringBuffer();
                sb.append(binding.activatedEt1.getText().toString());
                sb.append(binding.activatedEt2.getText().toString());
                sb.append(binding.activatedEt3.getText().toString());
                sb.append(binding.activatedEt4.getText().toString());
                String emailActivatedCode = ((RegisterActivity) context).getEmailActivatedCode();
                if(!emailActivatedCode.equals(sb.toString())){
                    binding.activatedEt1.setText("");
                    binding.activatedEt2.setText("");
                    binding.activatedEt3.setText("");
                    binding.activatedEt4.setText("");
                    binding.activatedEt1.requestFocus();

                    final ActivatedErrorPopupWindow activatedErrorPopupWindow = new ActivatedErrorPopupWindow(context);
                    activatedErrorPopupWindow.showPopupWindow(anchor);
                    new CountDownTimer(3000,1000){
                        @Override
                        public void onTick(long l) {}
                        @Override
                        public void onFinish() {
                            if(activatedErrorPopupWindow.isShowing()){
                                activatedErrorPopupWindow.dismiss();
                            }
                        }
                    }.start();

                }
                break;
        }
    }

    private class ActivatedTextWatcher implements TextWatcher{
        private int index;

        public ActivatedTextWatcher(int index) {
            this.index = index;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        @Override
        public void afterTextChanged(Editable editable) {
            if (!TextUtils.isEmpty(editable.toString()) && index < codes.length -1) {
                codes[index+1].requestFocus();
            }
            if(TextUtils.isEmpty(editable.toString()) && index > 0){
                codes[index-1].requestFocus();
            }
            String tempStr = "";
            for (int i = 0; i < codes.length; i++) {
                tempStr+=codes[i].getText().toString();
            }
            binding.activatedConfirmBtn.setEnabled(tempStr.length() == codes.length);
        }
    }
}
